#include "newthread645.h"
#include "QDebug"
#include "global_var.h"
#include <QTime>
newThread645::newThread645()
{

}
void newThread645::run()
{
    static  int idx = 0,readCnt=0;
    addr_cnt =0;
    QString str ,addr;
    memset(&addr_no,0,sizeof(addr_no));
        while(1)
        {
            QThread::msleep(100);
            if(idx++ == 20)
            {
                idx =0;
                if(need_read_meter_doc_next_flag ==0xAA)//读后续帧优先处理。
                {
                    item= protocol645Imp.prepareSend(READDOCNEXT, NULL,0,next_frame_no);    //读表档案,带后续帧
                }
                else if(readCnt == 0)  //第一次读地址，然后读档案。
                {
                    item= protocol645Imp.prepareSend(READADDR,NULL,0,0);   //触发准备发送645报文。
                }
                else if(readCnt%100 == 1)//读表档案。
                {
                    item= protocol645Imp.prepareSend(READDOC, NULL,0,0);    //读表档案
                }
                else
                {
                    if(addr_cnt !=0)
                    {
                         item= protocol645Imp.prepareSend(READDATE,addr_no,addr_cnt,0);  //读数据
                    }
                    else
                    {
                        addr.clear();
                        addr.append("没有监测单元--请插入监测单元,下面开始轮询通信单元的数据");
                        emit sendShowContent(addr);
                        item= protocol645Imp.prepareSend(READDATE_TX,addr_no_Comm,1,0);  //读数据
                    }

                }
                addr.clear();
                for(int i=0;i<6;i++)
                {
                  //  trans.append(QString::number(static_cast<uchar>(res[i]), 16));
                    addr.append(QString("%1").arg(static_cast<uchar>(item.addr[i]), 2, 16, QLatin1Char('0')));
                    addr.append(' ');
                }
                str = "==>抄读的地址是+++++"+QTime:: currentTime (). toString ( "HH:mm:ss" )+" "+addr+"+++++抄读数据项是 "+item.item.itemName;
                emit sendframeSignal_param_page();
                emit sendShowContent(str);
                readCnt++;
            }
        }
}
void newThread645::updatePageShowSlot()
{
    QString str,addr;
    addr.clear();
    for(int i=0;i<serial_channel.receive_len;i++)
    {
        addr.append(QString("%1").arg(static_cast<uchar>( serial_channel.recevice_frame_waite[i]), 2, 16, QLatin1Char('0')));
        addr.append(' ');
    }
    str = "<==返回的内容是"+QTime:: currentTime (). toString ( "HH:mm:ss" )+"  "+addr;
    emit sendShowContent(str);
    for(int i=10;i<serial_channel.receive_len;i++)
    {
        serial_channel.recevice_frame_waite[i] = serial_channel.recevice_frame_waite[i]-0x33;
    }
    addr.clear();
    for(int i=0;i<serial_channel.receive_len;i++)
    {
        addr.append(QString("%1").arg(static_cast<uchar>( serial_channel.recevice_frame_waite[i]), 2, 16, QLatin1Char('0')));
        addr.append(' ');
    }
/*
 *     Item_str   item645[ITEMAX] = {{{0x02,0x01,0xFF,0x00 }, "电压" },  //1
                                  {{0x02,0x02,0xFF,0x00 },"电流 "},  //2
                                  {{0x02,0x03,0xFF,0x00 },"瞬时有功功率 "},  //3
                                  {{0x02,0x04,0xFF,0x00 },"瞬时无功功率 "},  //4
                                  {{0x02,0x05,0xFF,0x00 },"瞬时视在功率 "},  //5
                                  {{0x02,0x06,0xFF,0x00 }," 功率因数 "},  //6
 * */
    INT32U item = (((INT8U)serial_channel.recevice_frame_waite[13])<<24)+(((INT8U)serial_channel.recevice_frame_waite[12])<<16)+
            (((INT8U)serial_channel.recevice_frame_waite[11])<<8)+(((INT8U)serial_channel.recevice_frame_waite[10])<<0);

    //查找监测单元645数据项的中文注释
    QString returnItem = "&&没找到对应的数据项&&";
    for(int i=0; i<ITEMAX;i++)
    {
        if(item == (((INT8U)item645_JC[i].item[0]<<24)+((INT8U)item645_JC[i].item[1]<<16)
                    +((INT8U)item645_JC[i].item[2]<<8)+((INT8U)item645_JC[i].item[3]<<0)))
        {
            returnItem =item645_JC[i].itemName;
            break;
        }
    }
    //寻找通信单元的item
    for(int i=0; i<ITEMAX_TX;i++)
    {
        if(item == (((INT8U)item645_TX[i].item[0]<<24)+((INT8U)item645_TX[i].item[1]<<16)
                    +((INT8U)item645_TX[i].item[2]<<8)+((INT8U)item645_TX[i].item[3]<<0)))
        {
            returnItem =item645_TX[i].itemName;
            break;
        }
    }

    str ="<---返回的内容是"+returnItem+"-33后是"+addr;
    emit sendShowContent(str);


    //添加表档案功能。
    if(((INT8U)serial_channel.recevice_frame_waite[8] == 0x93) &&(serial_channel.recevice_frame_waite[9] == 0x06))  //读回来的是地址。
    {
        mem_cpy_reverse((INT8U *)&addr_no_Comm[0][0],(INT8U *)&serial_channel.recevice_frame_waite[10],6);
    }


    if((((INT8U)serial_channel.recevice_frame_waite[8] == 0x91)||((INT8U)serial_channel.recevice_frame_waite[8] == 0xB1))&&
            ((INT8U)serial_channel.recevice_frame_waite[10] == 0x01)&&
            ((INT8U)serial_channel.recevice_frame_waite[11] == 0x03)&&
            ((INT8U)serial_channel.recevice_frame_waite[12] == 0xA0)&&
            ((INT8U)serial_channel.recevice_frame_waite[13] == 0x04))
    {
        addr_cnt =0;
        //读表档案的报文回复
        int docCnt = (INT8U)serial_channel.recevice_frame_waite[14];
        str = "=================================================";
        emit sendShowContent(str);
        str = "<---读回的表档案的个数是"+QString::number(docCnt);
        emit sendShowContent(str);
        INT8U meterDoc_Cnt=0,ADDR_Start_Pos=0;
        meterDoc_Cnt =  ((INT8U)serial_channel.recevice_frame_waite[9] -5) /7;
        ADDR_Start_Pos=15;
        for(int idx=0;idx<meterDoc_Cnt;idx++)
        {
            mem_cpy_reverse((INT8U *)&addr_no[addr_cnt][0],(INT8U *)&serial_channel.recevice_frame_waite[ADDR_Start_Pos+idx*7],6);
            addr_cnt++;
            addr.clear();
            for(int i=0;i<6;i++)
            {
                addr.append(QString("%1").arg(static_cast<uchar>( serial_channel.recevice_frame_waite[ADDR_Start_Pos+idx*7+5-i]), 2, 16, QLatin1Char('0')));
                addr.append(' ');
            }
            str = "<---第"+QString::number(idx+1)+"个表档案是"+addr;
            emit sendShowContent(str);
        }
//        addr_cnt = docCnt;
        str = "=================================================";
        emit sendShowContent(str);

        if((INT8U)serial_channel.recevice_frame_waite[8] == 0xB1)
        {
           need_read_meter_doc_next_flag =0xAA;
           next_frame_no=1;
           qDebug()<< "读表档案，当时有后续帧";
        }
        else
        {
            need_read_meter_doc_next_flag =0;
        }
    }
    //读后续帧
    if((((INT8U)serial_channel.recevice_frame_waite[8] == 0x92)||((INT8U)serial_channel.recevice_frame_waite[8] == 0xB2))&&
            ((INT8U)serial_channel.recevice_frame_waite[10] == 0x01)&&
            ((INT8U)serial_channel.recevice_frame_waite[11] == 0x03)&&
            ((INT8U)serial_channel.recevice_frame_waite[12] == 0xA0)&&
            ((INT8U)serial_channel.recevice_frame_waite[13] == 0x04))
    {

        INT8U meterDoc_Cnt=0,ADDR_Start_Pos=0;
        meterDoc_Cnt =  ((INT8U)serial_channel.recevice_frame_waite[9] -5) /7;
        ADDR_Start_Pos=14;
        for(int idx=0;idx<meterDoc_Cnt;idx++)
        {
            mem_cpy_reverse((INT8U *)&addr_no[addr_cnt][0],(INT8U *)&serial_channel.recevice_frame_waite[ADDR_Start_Pos+idx*7],6);
            addr_cnt++;
            addr.clear();
            for(int i=0;i<6;i++)
            {
                addr.append(QString("%1").arg(static_cast<uchar>( serial_channel.recevice_frame_waite[ADDR_Start_Pos+idx*7+5-i]), 2, 16, QLatin1Char('0')));
                addr.append(' ');
            }
            str = "<---第"+QString::number(idx+1)+"个表档案是"+addr;
            emit sendShowContent(str);
        }
//        addr_cnt = docCnt;
        str = "=================================================";
        emit sendShowContent(str);

        if((INT8U)serial_channel.recevice_frame_waite[8] == 0xB2)
        {
           need_read_meter_doc_next_flag =0xAA;
           next_frame_no++;
           qDebug()<< "读表档案，当时有后续帧";
        }
        else
        {
            need_read_meter_doc_next_flag =0;
        }
    }
    str = "----------------------------------------------------------------------------";
    emit sendShowContent(str);

}
